﻿@model UrlRecordListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.System.SeNames").Text;
}

@Html.AntiForgeryToken()
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-system.png")" alt="" />
        @T("Admin.System.SeNames")
    </div>
    <div class="options">
        <input type="button" id="delete-selected" class="k-button" value="@T("Admin.System.SeNames.DeleteSelected")" />
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SeName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SeName)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-senames" class="k-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<table class="adminContent">
    <tr>
        <td>
            <div id="sename-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#sename-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("SeNames", "Common"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        scrollable: false,
                        dataBound: onDataBound,
                        columns: [{
                            field: "Id",
                            headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                            width: 50
                        }, {
                            field: "Id",
                            title: "Id",
                            width: 50
                        }, {
                            field: "Name",
                            title: "@T("Admin.System.SeNames.Name")",
                            width: 300
                        }, {
                            field: "EntityId",
                            title: "@T("Admin.System.SeNames.EntityId")",
                            width: 50
                        }, {
                            field: "EntityName",
                            title: "@T("Admin.System.SeNames.EntityName")",
                            width: 100
                        }, {
                            field: "IsActive",
                            title: "@T("Admin.System.SeNames.IsActive")",
                            width: 50,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsActive#.gif" />'
                        }, {
                            field: "Language",
                            title: "@T("Admin.System.SeNames.Language")",
                            width: 100
                        }, {
                            field: "DetailsUrl",
                            title: "@T("Admin.System.SeNames.Details")",
                            width: 100,
                            template: '# if(DetailsUrl && DetailsUrl.length > 0) {# <a href="#=DetailsUrl#">@T("Admin.System.SeNames.Details")</a> #} #'
                        }]
                    });
                });
            </script>
        </td>
    </tr>
</table>
    
<script type="text/javascript">
    var selectedIds = [];

    $(document).ready(function() {

        //search button
        $('#search-senames').click(function() {
            //search
            var grid = $('#sename-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            //clear selected checkboxes
            $('.checkboxGroups').attr('checked', false).change();
            selectedIds = [];
            return false;
        });

        $("#@Html.FieldIdFor(model => model.SeName)").keydown(function(event) {
            if (event.keyCode == 13) {
                $("#search-senames").click();
                return false;
            }
        });

        //"delete selected" button
        $('#delete-selected').click(function(e) {
            e.preventDefault();

            var postData = {
                selectedIds: selectedIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("DeleteSelectedSeNames", "Common"))",
                data: postData,
                complete: function(data) {
                    //reload grid
                    var grid = $('#sename-grid').data('kendoGrid');
                    grid.dataSource.page(1); //new search. Set page size to 1
                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                    //clear selected checkboxes
                    $('.checkboxGroups').attr('checked', false).change();
                    selectedIds = [];
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                },
                traditional: true
            });
            return false;
        });

        $('#mastercheckbox').click(function() {
            $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
        });

        //wire up checkboxes. 
        $('#sename-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function(e) {
            var $check = $(this);
            if ($check.is(":checked") == true) {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked == -1) {
                    //add id to selectedIds.  
                    selectedIds.push($check.val());
                }
            } else {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked > -1) {
                    //remove id from selectedIds.  
                    selectedIds = $.grep(selectedIds, function(item, index) {
                        return item != $check.val();
                    });
                }
            }
            updateMasterCheckbox();
        });
    });

    function additionalData() {
        var data = {
            SeName: $('#@Html.FieldIdFor(model => model.SeName)').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

    function onDataBound(e) {

        $('#sename-grid input[type=checkbox][id!=mastercheckbox]').each(function() {
            var currentId = $(this).val();
            var checked = jQuery.inArray(currentId, selectedIds);
            //set checked based on if current checkbox's value is in selectedIds.  
            $(this).attr('checked', checked > -1);
        });

        updateMasterCheckbox();
    }

    function updateMasterCheckbox() {
        var numChkBoxes = $('#sename-grid input[type=checkbox][id!=mastercheckbox]').length;
        var numChkBoxesChecked = $('#sename-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
        $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
    }
</script>